﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System;
using System.Text;

public class DongfuView : MonoBehaviour
{
    public Image image;
    public Text name;
    public Text level;

    public Image bodyf;
    public Image bodyB;

    public Slider exSlider;
    public Text sliderText;

    public Text record;

    public Button gongGaoBtn;
    public Button jieYinBtn;

    private CharacterLevelConfigItem config;

    void OnShow()
    {
        config = CharacterLevelConfig.Instance.GetItem(CharacterModel.Instance.Level);
        MessageCenter.Instance.Register(MessageConsts.EX_UPDATE, OnUpdateEx);
        RefreshInfo();
        PlayCloudAnim();
        PlayDaZuoAnim();

        gongGaoBtn.onClick.RemoveAllListeners();
        jieYinBtn.onClick.RemoveAllListeners();

        gongGaoBtn.onClick.AddListener(() => { UIManager.Instance.gonggao.SetActive(true); });
        jieYinBtn.onClick.AddListener(() => { UIManager.Instance.ShowPage(UINames.JieYin); });
    }


    void OnHide()
    {
        if (tween != null)
        {
            tween.Kill();
        }
        MessageCenter.Instance.UnRegister(MessageConsts.EX_UPDATE, OnUpdateEx);
    }

    Tweener tween;
    private void PlayCloudAnim()
    {
        if (tween != null)
        {
            tween.Kill();
            tween = null;
        }
        image.transform.localPosition = new Vector3(-1000, 0, 0);
        tween = image.transform.DOLocalMoveX(2000, UnityEngine.Random.Range(5, 10));
        tween.onComplete += () => { PlayCloudAnim(); };
    }


    private void OnUpdateEx(object obj)
    {
        if (!gameObject.activeInHierarchy)
            return;
        config = CharacterLevelConfig.Instance.GetItem(CharacterModel.Instance.Level);
        AlertBubble.Show(CharacterModel.Instance.m_RecordList[CharacterModel.Instance.m_RecordList.Count - 1]);
        RefreshInfo();
    }
    Tweener tweener2;
    private void PlayDaZuoAnim()
    {
        if (tweener2 == null)
        {
            tweener2.Kill();
            tweener2 = null;
        }
        DOVirtual.Float(0, 0.5f, 2f, (t) => { bodyf.color = new Color(1, 1, 1, Mathf.Sin(t * Mathf.PI)); }).SetLoops(int.MaxValue, LoopType.Yoyo);
    }

    private void RefreshInfo()
    {
        name.text = config.name;
        level.text = config.level.ToString();
        exSlider.value = (float)CharacterModel.Instance.Ex / config.levelUp;
        sliderText.text = string.Format("{0}/{1}", CharacterModel.Instance.Ex, config.levelUp);

        RefreshRecord();
    }

    private void RefreshRecord()
    {
        StringBuilder sb = new StringBuilder();
        bool first = true;
        foreach (var text in CharacterModel.Instance.m_RecordList)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                sb.AppendLine();
            }
            sb.Append(text);
        }
        record.text = sb.ToString();
        var pos = record.transform.localPosition;
        var y = record.rectTransform.rect.height - 320;
        if (y < 0)
            y = 0;
        record.transform.localPosition = new Vector3(pos.x, y, pos.z);
    }
}
